﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Prism.Games.GalEngine.Scripts.Data.SceneElements;
using System.IO;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class VocalParser : ISceneParser
    {
        public VocalParser()
        {
            this.preVocal = new Vocal() { Id = -1 };

            this.VocalManager = new Audio.AudioManager(Helper.AudioBase.AudioFactory, Helper.AudioBase.VocalPlaybackLatency);
            this.VocalManager.EnableSingleChannel = true;
        }

        public void Restart()
        {
            this.VocalManager.Reset();
            this.preVocal = new Vocal() { Id = -1 };
        }

        public Vocal preVocal { get; protected set; }

        public Audio.AudioManager VocalManager { get; protected set; }

        public void ChangeVolume(float volume)
        {
            this.VocalManager.Volume = volume;
        }


        public void PresentObject(FunctionModel.SceneObject element)
        {
            if (element is Vocal)
            {
                var target = element as Vocal;

                if (target.IsCached)
                {
                    this.VocalManager.Reset();

                    this.VocalManager.Load(target.CacheStream, target.Id);

                    this.VocalManager.BeginPlay(target.Id);

                    preVocal = target;
                }
                //else
                //{

                //    if (target.PartUri != null)
                //    {
                //        this.VocalManager.Reset();

                //        using (var ms = Helper.Packer.GetPartSteam(target.PartUri))
                //        {
                //            var buffer = new byte[ms.Length];
                //            ms.Read(buffer, 0, (int)ms.Length);
                //            this.VocalManager.Load(buffer, target.Id);
                //        }

                //        this.VocalManager.BeginPlay(target.Id);

                //        preVocal = target;
                //    }
                //}
            }
        }
    }
}
